package com.wgo.precise.client.ui.view.editor.properties;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.wgo.precise.client.ui.controller.Registry;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.facade.ILabelProviderItem;
import com.wgo.precise.client.ui.view.util.PropertyLinkDescriptorLabelProvider;


/**
 * @author petterei
 * @version $Id: PropertyLinkDescriptor.java,v 1.2 2006-02-06 09:29:08 petterei Exp $
 */
public class PropertyLinkDescriptor extends PropertyDescriptor implements IPropertyDescriptor {
	private ILabelProviderItem item = null;
	public PropertyLinkDescriptor(ILabelProviderItem item) {
		super(item, "");
		this.item = item;
		this.setLabelProvider(new PropertyLinkDescriptorLabelProvider());
	}

	@Override
	public CellEditor createPropertyEditor(Composite parent) {
		CellEditor editor = new CellEditor(parent) {
			@Override
			protected Control createControl(@SuppressWarnings("hiding") Composite parent) {
				Hyperlink link = new Hyperlink(parent, SWT.WRAP);
				link.addHyperlinkListener(new HyperlinkAdapter() {
					@Override
					public void linkActivated(@SuppressWarnings("unused") HyperlinkEvent e) {
						RequirementPlugin.getInstance().getEditorWindowManager().startEditorPart(Registry.MODEL_EDITOR, (ClientModelItem)item);
					}
				});
				link.setText(" " + item.getSimpleName());
				return link;
			}
			@Override
			protected Object doGetValue() {
				return getControl();
			}
			@Override
			protected void doSetFocus() {
				getControl().setFocus();
			}
			@Override
			protected void doSetValue(@SuppressWarnings("unused") Object value) {
			}
			@Override
			public void activate() {
				getControl().setVisible(true);
			}
		};
		editor.activate();
		return editor;
	}
	public ILabelProviderItem getItem() {
		return item;
	}
}
